package com.jiuyou.storeController.comment;

import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.comment.domain.Comment;
import com.ruoyi.comment.service.ICommentService;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author chengliang
 * @date 2021/11/25
 **/
@RestController
@RequestMapping("/store/comment")
@Api(tags = {"评论管理控制器"})
public class CommentStoreController extends StoreBaseController {
    @Autowired
    private ICommentService commentService;

    @PostMapping("/reply")
    @ApiOperation("回复评论")
    public Comment reply(@ApiParam @RequestBody Comment comment){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            throw new CustomException("权限不足");
        }
        comment.setUpdateBy(getLoginStoreUserId());
        comment.setUpdateType(DicConstants.BUSINESS_Business.getCode());
        commentService.updateComment(comment);
        return comment;
    }
    @PostMapping
    @ApiOperation("获取列表")
    public AjaxResult list(Integer pageNum, Integer pageSize,String type)
    {
        StoreUserRole role = getStoreUserRole();

        Comment commentParam=new Comment();
        commentParam.setStoreId(role.getStoreId());
        startPage();
        switch (type){
            case "good":
                commentParam.setStoreLevel(new BigDecimal("5"));
                break;
            case "medium":
                commentParam.setMaxLevel(new BigDecimal("5"));
                commentParam.setMinLevel(new BigDecimal("3"));
                break;
            case "bad":
                commentParam.setMaxLevel(new BigDecimal("3"));
                break;
            case "waitBad":
                commentParam.setReplyState("need");
                commentParam.setMaxLevel(new BigDecimal("3"));
                break;
        }
        List<Comment> list = commentService.selectCommentList(commentParam);
        TableDataInfo tableDataInfo =getDataTable(list);
        return AjaxResult.success(tableDataInfo);
    }
    @PostMapping("/getDetail/{commentId}")
    @ApiOperation("获取评论详情")
    public Comment getDetail(@PathVariable Long commentId){
        StoreUserRole role = getStoreUserRole();
        if(role == null){
            return null;
        }
        return commentService.selectCommentById(commentId);
    }

}
